package com.das.mechanic_base.utils;

import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.util.Log;

import androidx.annotation.NonNull;

import com.das.mechanic_base.app.X3MyApp;

public class LanguageResources extends Resources {

    private Resources resources;

    public LanguageResources(Resources resources) {
        super(resources.getAssets(), resources.getDisplayMetrics(), resources.getConfiguration());
        this.resources = resources;
    }

    /**
     * Create a new Resources object on top of an existing set of assets in an
     * AssetManager.
     *
     * @param assets  Previously created AssetManager.
     * @param metrics Current display metrics to consider when
     *                selecting/computing resource values.
     * @param config  Desired device configuration to consider when
     * @deprecated Resources should not be constructed by apps.
     * See {@link Context#createConfigurationContext(Configuration)}.
     */
    public LanguageResources(AssetManager assets, DisplayMetrics metrics, Configuration config) {
        super(assets, metrics, config);
    }

    @NonNull
    @Override
    public CharSequence getText(int id) throws NotFoundException {
        if (id == 0) {
            return "";
        }
        if (X3MyApp.strMap == null) {
            return resources.getText(id);
        }
        String resourceEntryName = resources.getResourceEntryName(id);
        Object o = X3MyApp.strMap.get(resourceEntryName);
        if (o == null) {
            return getStringByMap(resourceEntryName);
        }
        return resources.getText(id);
    }

    @Override
    public CharSequence getText(int id, CharSequence def) {
        if (id == 0) {
            return "";
        }
        if (X3MyApp.strMap == null) {
            return resources.getText(id,def);
        }
        String resourceEntryName = resources.getResourceEntryName(id);
        Object o = X3MyApp.strMap.get(resourceEntryName);
        if (o == null) {
            return String.format((String) o,def);
        }
        return resources.getText(id, def);
    }

    private String getStringByMap(String resourceEntryName) {
        Object o = X3MyApp.strMap.get(resourceEntryName);
        if (o == null) {
            return null;
        }
        return ((String)X3MyApp.strMap.get(resourceEntryName))
                .replaceAll("&#160;"," ")
                .replaceAll("\\\\n","\n")
                .replaceAll("&amp;","&")
                .replaceAll("\\\\","");
    }

    @NonNull
    @Override
    public String getString(int id) throws NotFoundException {
        if (id == 0) {
            return "";
        }
        if (X3MyApp.strMap == null) {
            return resources.getString(id);
        }
        String resourceEntryName = resources.getResourceEntryName(id);
        Object o = getStringByMap(resourceEntryName);

        if (o != null) {
            return (String) o;
        }
        String  newResourceEntryName= resourceEntryName.replaceAll("x3_", "");
        o = getStringByMap(newResourceEntryName);
        if (o != null) {
            return (String) o;
        }
        return resources.getString(id);
    }

    @NonNull
    @Override
    public String getString(int id, Object... formatArgs) throws NotFoundException {
        if (id == 0) {
            return "";
        }
        if (X3MyApp.strMap == null) {
            return resources.getString(id,formatArgs);
        }
        String resourceEntryName = resources.getResourceEntryName(id);
        Object o = getStringByMap(resourceEntryName);
        if (o != null) {
            return String.format((String) o,formatArgs);
        }
        String newResourceEntryName= resourceEntryName.replaceAll("x3_", "");
        o = getStringByMap(newResourceEntryName);
        if (o != null) {
            return String.format((String) o,formatArgs);
        }
        return resources.getString(id, formatArgs);
    }

    @Override
    public void updateConfiguration(Configuration config, DisplayMetrics metrics) {
        if (resources == null) {
            super.updateConfiguration(config,metrics);
        }else {
            resources.updateConfiguration(config,metrics);
        }
    }
}